package edu.cdbi.view;

import java.awt.Container;
import java.util.Collection;

import javax.swing.JComboBox;
import javax.swing.JTextField;

public class BasicComponent {

	public final static byte  TYPE_TEXT_FIELD = 0;
	public final static byte  TYPE_COMBO_BOX = 1;
	
	private byte viewType;
	private boolean editable;
	
	private JComboBox combox;
	private Collection<String> choices;
	private JTextField text;
	
	public BasicComponent(byte viewType, Collection<String> choices, boolean editable){
		this.editable = editable;
		initial();
		this.viewType = viewType;
		this.choices = choices;
		if(choices == null){
			return;
		}else{
			for(String str : this.choices){
				combox.addItem(str);
			}
		}
	}
	
	public BasicComponent(byte viewType, String text, boolean editable){
		this.editable = editable;
		initial();
		this.viewType = viewType;
		this.text.setText(text);
	}
	
	public BasicComponent(byte viewType, boolean editable){
		this.editable = editable;
		initial();
		this.viewType = viewType;
		this.text.setText("");
	}
	
	public BasicComponent(){
		this.editable = true;
		initial();
		this.viewType = TYPE_TEXT_FIELD;
		this.text.setText("");
	}
	
	private void initial(){
		combox = new JComboBox();
		text = new JTextField("");
		combox.setEditable(editable);
		text.setEditable(editable);
	}
	
	public String getText(){
		if(viewType == TYPE_COMBO_BOX){
			if(combox.getItemCount() > 0){
				return (String)combox.getSelectedItem();
			}
		}else if(viewType == TYPE_TEXT_FIELD){
			return text.getText();
		}
		
		return null;
	}
	
	public void setText(String string){
		if(viewType == TYPE_COMBO_BOX){
			if(combox.getItemCount() > 0){
				combox.setSelectedItem(string);
			}
		}else if(viewType == TYPE_TEXT_FIELD){
			text.setText(string);
		}
	}
	
	public Container getContainer(){
		if(viewType == TYPE_COMBO_BOX){
			return combox;
		}else if(viewType == TYPE_TEXT_FIELD){
			return text;
		}else{
			return null;
		}
	}
	
	public byte getViewType(){
		return viewType;
	}
}
